package com.starnet.yarnmonitor.yarn.handler;

import com.starnet.yarnmonitor.yarn.vo.result.Result;
import lombok.extern.log4j.Log4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;

/***
 * 全局异常统一处理类
 */
@Log4j
@RestControllerAdvice
public class ExceptionControllerAdvice {
    //参数不合法异常
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        System.out.println(e.getMessage());
        return Result.fail("参数错误");
    }
    @ExceptionHandler(ApiException.class)
    public Result ApiExceptionHandler(ApiException e){
        return Result.fail(e.getMsg());
    }

    @ExceptionHandler(IOException.class)
    public Result IOExceptionHandler(IOException e){
        log.error(e.getMessage());
        System.out.println(e.getMessage());
        return Result.fail("IO异常，可能是宕机了");
    }
}
